package com.binance.amazon.s3.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

@Configuration
@Log4j2
public class AmazonRekognitionConfig implements EnvironmentAware {

    private Environment environment;

    @Value("${amazon.rekognition.region:null}")
    private String region;

    @Value("${amazon.rekognition.accesskey:null}")
    private String accessKey;

    @Value("${amazon.rekognition.secretkey:null}")
    private String secretKey;

//    @Value("${amazon.rekognition.collection.id}")
//    private String collectionId;



    @Bean
    public AmazonRekognition amazonRekognition() {
        if (StringUtils.isEmpty(region)) {
            log.warn("no region configured.");
            return null;
        }
        AmazonRekognitionClientBuilder builder = AmazonRekognitionClientBuilder.standard().withRegion(region);
        if (StringUtils.isEmpty(accessKey) || StringUtils.isEmpty(secretKey)) {
            log.info("accessKey或secretKey为空, EC2ContainerCredentialsProviderWrapper");
            builder.withCredentials(new EC2ContainerCredentialsProviderWrapper());
        } else {
            log.info("accessKey和secretKe不为空, 使用AWSStaticCredentialsProvider认证");
            builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
        }
        return builder.build();
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
